﻿#include "FITKAbaLoadBodyForce.h"
#include "FITKAbaStepManager.h"


Interface::FITKAbaAbstractLoad::FITKAbaLoadType Interface::FITKAbaLoadBodyForce::getLoadType() const
{
    return Interface::FITKAbaAbstractLoad::FITKAbaLoadType::BodyForce;
}

bool Interface::FITKAbaLoadBodyForce::isModifiedInStep(int stepId) const
{
    //判断在分析步stepId中数据是否被修改
    if (m_modifiedDatas.contains(stepId))
        return true;
    return false;
}

void Interface::FITKAbaLoadBodyForce::activeStateChanged()
{
    //在分析步中的激活状态发生变化时，对数据各分析步中的数据进行调整,激活时，不做处理
    if (m_deactiveStepId == -1)
        return;
    //取消激活时，清除后续分析步数据
    //访问当前算例的分析步管理器，获取分析步顺序id列表。
    QVector<int> stepIds = this->getStepIds();
    int index = stepIds.indexOf(m_deactiveStepId);
    if (index == -1)
        return;
    for (int i = index ; i < stepIds.count(); ++i)
    {
        int id = stepIds[i];
        if (m_modifiedDatas.contains(id))
            m_modifiedDatas.remove(id);
    }
}

bool Interface::FITKAbaLoadBodyForce::copy(FITKAbstractDataObject *obj)
{
    bool ok = FITKAbaAbstractLoad::copy(obj);
    if (!ok)
        return false;
    FITKAbaLoadBodyForce* force = dynamic_cast<FITKAbaLoadBodyForce*>(obj);
    if (force == nullptr)
        return false;
    this->m_data = force->m_data;
    this->m_modifiedDatas = force->m_modifiedDatas;
    return true;
}

double Interface::FITKAbaLoadBodyForce::getComponent1(int stepId)
{
    return this->getDataInStep(stepId).getComponent1();
}

void Interface::FITKAbaLoadBodyForce::setComponent1(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setComponent1(value);
    else
    {
        if (m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setComponent1(value);
    }
}

double Interface::FITKAbaLoadBodyForce::getComponent2(int stepId)
{
    return this->getDataInStep(stepId).getComponent2();
}

void Interface::FITKAbaLoadBodyForce::setComponent2(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setComponent2(value);
    else
    {
        if (m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setComponent2(value);
    }
}

double Interface::FITKAbaLoadBodyForce::getComponent3(int stepId)
{
    return this->getDataInStep(stepId).getComponent3();
}

void Interface::FITKAbaLoadBodyForce::setComponent3(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setComponent3(value);
    else
    {
        if (m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setComponent3(value);
    }
}

int Interface::FITKAbaLoadBodyForce::getAmplitudeId(int stepId)
{
    return this->getDataInStep(stepId).getAmplitudeId();
}

void Interface::FITKAbaLoadBodyForce::setAmplitudeId(int id, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setAmplitudeId(id);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setAmplitudeId(id);
    }
}

int Interface::FITKAbaLoadBodyForce::getAnalyticalFieldId() const
{
    return m_analyticalFieldId;
}

void Interface::FITKAbaLoadBodyForce::setAnalyticalFieldId(int id)
{
    m_analyticalFieldId = id;
}

Interface::BodyForceData Interface::FITKAbaLoadBodyForce::getDataInStep(int stepId)
{
    if (stepId <= 0)
        return m_data;
    if (m_modifiedDatas.contains(stepId))
        return m_modifiedDatas[stepId];
    QVector<int> stepIds = this->getStepIds();
    int createdStepIndex = stepIds.indexOf(m_createdStepId);
    int stepIndex = stepIds.indexOf(stepId);
    if (!(createdStepIndex >= 0 && createdStepIndex < stepIds.size()))
        return m_data;
    if (!(stepIndex >= 0 && stepIndex < stepIds.size()))
        return m_data;
    for (int i = stepIndex; i >= createdStepIndex; --i)
    {
        int id = stepIds.at(i);
        if (m_modifiedDatas.contains(id))
            return m_modifiedDatas[id];
    }
    return m_data;
}

double Interface::BodyForceData::getComponent1() const
{
    return m_value[0];
}

void Interface::BodyForceData::setComponent1(double value)
{
    m_value[0] = value;
}

double Interface::BodyForceData::getComponent2() const
{
    return m_value[1];
}

void Interface::BodyForceData::setComponent2(double value)
{
    m_value[1] = value;
}

double Interface::BodyForceData::getComponent3() const
{
    return m_value[2];
}

void Interface::BodyForceData::setComponent3(double value)
{
    m_value[2] = value;
}

int Interface::BodyForceData::getAmplitudeId() const
{
    return m_amplitudeId;
}

void Interface::BodyForceData::setAmplitudeId(int id)
{
    m_amplitudeId = id;
}
